package com.ly.mpayment.rest.up;

import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.UpOrder;
import com.ly.mpayment.persist.entity.UpShopInfo;
import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.mobile.IMobileService;
import com.ly.mpayment.persist.service.uporder.IUporderService;
import com.ly.mpayment.persist.service.upshopinfo.IUpShopInfoService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
 * 上游订单统一回调地址
 */
@Component
@Path("order")
public class UpCallBackRest {

    Logger logger = Logger.getLogger(UpCallBackRest.class);

    @Autowired
    IUporderService upOrderService;

    @Autowired
    IDownRechargeFlowService flowService;

    @Autowired
    IDownShopInfoService shopService;

    @Autowired
    IUpShopInfoService upShopInfoService;

    @Autowired
    IMobileService mobileService;

    @Autowired
    OrderHandler orderHandler;


    @Path("callBack")
    @GET
    public synchronized String getCallBack(@PathParam("orderId") String orderId){
        logger.info("getCallBack:"+orderId);
        return "failt";
    }

    /**
     * 统一回调地址
     * @param body
     * @return
     */
    @Path("callBack")
    @POST
    public String callBack(@RequestBody String body){
        logger.info("postcallBack:"+body);
        try{
            if(!StringUtils.isBlank(body)){
                JSONObject jsonObject = JSONObject.fromObject(body);
                final UpOrder order = upOrderService.findById(jsonObject.getString("batchNo"));
                if(order == null){
                    logger.info("订单不存在或已经处理:"+body);
                    return "failt";
                }
                final UpShopInfo upShopInfo = this.upShopInfoService.findByUserId(order.getUserOrder().getUseruuid());
                if(upShopInfo == null){
                    logger.info("渠道用户不存在:"+order.getUserOrder().getUseruuid());
                    return "failt";
                }
                //签名
                switch (upShopInfo.getShopnum()){
                    default:
                        logger.info("未配置渠道:"+body);
                        break;
                }
                return "success";
            }
            return "failt";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "failt";

    }
}
